<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

require_once(dirname(__FILE__) . "/../common/Logger.php");
require_once(dirname(__FILE__) . "/../common/Common.php");
require_once(dirname(__FILE__) . "/../dbcom/WeiXinCom.php");
require_once(dirname(__FILE__) . "/../dbcom/CustomerCom.php");

$log = new Logger("./../loginapi.log");
$common = new Common();
$weixincom = new WeiXinCom();
$customercom = new CustomerCom();

$rawdata = $_REQUEST['rawData']?$_REQUEST['rawData']:"";
$signature = $_REQUEST['signature']?$_REQUEST['signature']:"";
$encrypteddata = $_REQUEST['encryptedData']?$_REQUEST['encryptedData']:"";
$code = $_REQUEST['code']?$_REQUEST['code']:"";
$iv = $_REQUEST['iv']?$_REQUEST['iv']:"";

//测试数据
//$rawdata = '{"nickName":"孙海龙","gender":1,"language":"zh_CN","city":"Baoshan","province":"Shanghai","country":"China","avatarUrl":"https://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLdks0jRqxgaJIpIcMbBVNWgjaiad2e9d9PHp8yjPGficttib6EUeQw9U1Q98dTFia9BUfniaodT5MuONg/132"}';
//$signature = "dd29f010bc4f57ab174aef";
//$encrypteddata = "ARNCqJLKdpm5cKYDKvfOigkG8MeOCgbblRtDn71NyoMLq2flNfjHoUjPcxcVFIsZAWrFy4RKG6CBjIn9znv8v/Uu+OBZ8cLiPf9yzvwCmoDLkkMCjXrv6yZHYCVAM9VJFoiCmyoanizAmCdREP61WWjvUC/r+a3y/qFv8GUvkmxqbuWzVw7f3Q74pMHubAae5Q57b4bgAngxx6PEUZJUCll3RzrxWbqeLNgPwRytXiO7BGmTHd6Eoz9MH4w2/n/gZ/vFYgy2H0OkNwuw5adROzwEzgdp4wHvNFwUKetyPienrCWoTpahgvKBxAmlujO37RtZF5vAxTIetmV8Pmh6ZcSNLbs4oQIYOscosYbycYpKYCuj74OC43xJDJ/4hN2T+EWDUQGuWRLwC/SOwKZoIcFlZvW1fS5tLOmW9iAvFGh459at9vTRZkOaQNngkt9k0GHGUzzBSQRLjrz94xwncFoHWsIULUpja28YTUtvV5E=";
//$code = "001vn6702G90OZ0u0D502vP0702vn67x";
//$iv = "cQZAHIoPi+phTg+OusXa4g==";

//echo $code."</br>";

$log->debug("START LOG");
$log->debug("rawdata:".$rawdata);
$log->debug("signature:".$signature);
$log->debug("encrypteddata:".$encrypteddata);
$log->debug("code:".$code);
$log->debug("iv:".$iv);

if(empty($rawdata) || empty($signature) || empty($encrypteddata) || empty($iv) || empty($code)){
    echo json_encode(array("code"=>100,"msg"=>"lose parameter"));
    exit();
}

$weixinapp = $weixincom->getinfo(1); 
$codeurl = "https://api.weixin.qq.com/sns/jscode2session?appid=".$weixinapp['appid']."&secret=".$weixinapp['appsecret']."&js_code=".$code."&grant_type=authorization_code";
//echo $codeurl."</br>";
$result = $common->curl_get($codeurl);
//var_dump($result);

if(!isset($result['session_key'])){
    echo json_encode(array("code"=>150,"msg"=>"cannot find session_key"));
    exit();
}

$sessionkey = $result['session_key'];
$openid = $result['openid'];

$signature1 = sha1($rawdata.$sessionkey);
$log->debug("mysign:".$signature1);

if($signature != $signature1){
    echo json_encode(array("code"=>200,"msg"=>"signature error"));
    exit();
}

//开始登录
$userdata = json_decode($rawdata,true);
$log->debug("GetUserData:". json_encode($userdata));
//var_dump($userdata);
if(empty($userdata)){
    echo json_encode(array("code"=>500,"msg"=>"cannnot find userinfo"));
    exit();
}

$userid = $customercom->checkOpenid($openid);
if(!$userid){
    $userid = $customercom->addCustomer($userdata, $openid);
    if(!$userid){
        echo json_encode(array("code"=>400,"msg"=>"addcustomer failed"));
        exit();
    }
}

echo json_encode(array("code"=>0,"msg"=>"success","data"=>array("userid"=>$userid)));
exit();
